package com.flea.hclv.service.impl;

import com.flea.hclv.constant.HConstant;
import com.flea.hclv.model.dao.HSpotMapper;
import com.flea.hclv.model.dao.SpotTicketMapper;
import com.flea.hclv.model.entity.HSpot;
import com.flea.hclv.model.entity.HTicketDeptOrder;
import com.flea.hclv.model.entity.SpotTicket;
import com.flea.hclv.service.HSpotTicketService;
import com.flea.hclv.util.ServerResponse;
import com.flea.system.base.util.StringUtil;
import com.flea.system.bean.FleaPage;
import com.flea.system.dao.OrgDeptMapper;
import com.flea.system.entity.OrgDept;
import com.flea.system.platform.session.FleaOrgUtils;
import com.flea.system.utils.FleaStringUtils;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class HSpotTicketServiceImpl implements HSpotTicketService {

    @Autowired
    private SpotTicketMapper spotTicketMapper;
    @Autowired
    private HSpotMapper hSpotMapper;
    @Autowired
    private OrgDeptMapper orgDeptMapper;

    @Override
    public ServerResponse<FleaPage<SpotTicket>> selectBySpotId(String spotId, int curPage, int pageSize, String deptUuid) {
        RowBounds bounds = new RowBounds(curPage,pageSize);
        if (StringUtils.isEmpty(spotId)){
            try {
                throw new Exception("spotId为空");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        HSpot hSpot = hSpotMapper.selectByPrimaryKey(spotId);
        List<SpotTicket> spotTicketList = Lists.newArrayList();
        List<SpotTicket> list = spotTicketMapper.selectBySpotId(spotId,bounds,deptUuid);
        FleaPage<SpotTicket> spotTicketFleaPage = new FleaPage<>(list);
        if (CollectionUtils.isEmpty(list)){
            return ServerResponse.createBySuccess(new FleaPage<>(list));
        }
        for (SpotTicket aList : list) {
            SpotTicket ticket = new SpotTicket();
            ticket.setId(aList.getId());
            ticket.setDescription(aList.getDescription());
            ticket.setUuid(aList.getUuid());
            ticket.setCreateUser(aList.getCreateUser());
            ticket.setModifyUser(aList.getModifyUser());
            ticket.setCode(aList.getCode());
            ticket.setCreateTime(aList.getCreateTime());
            ticket.setModifyTime(aList.getModifyTime());
            ticket.setValid(aList.getValid());
            ticket.setLabel(aList.getLabel());
            ticket.setNumber(aList.getNumber());
            ticket.setPrice(aList.getPrice());
            ticket.setSalePrice(aList.getSalePrice());
            ticket.setType(aList.getType());
            ticket.setSpotName(hSpot.getName());

            spotTicketList.add(ticket);
        }
        spotTicketFleaPage.setCurPageData(spotTicketList);
        spotTicketFleaPage.setAllDataCount((int) spotTicketFleaPage.getAllDataCount());
        return ServerResponse.createBySuccess(spotTicketFleaPage);
    }
    @Override
    public ServerResponse<FleaPage<SpotTicket>> selectByDeptId(String deptUuid, int curPage, int pageSize, String spotId,String spotName) {
        RowBounds rowBounds = new RowBounds(curPage,pageSize);
//        List<String> spotIds = Lists.newArrayList();
//        Map<String,String> names = Maps.newHashMap();
//        if (StringUtils.isNotBlank(spotId)){
//            HSpot spot = hSpotMapper.selectByPrimaryKey(spotId);
//            spotIds.add(spotId);
//            names.put(spot.getId(),spot.getName());
//        }else{
//            List<HSpot> hSpotList = hSpotMapper.selectByDeptId(deptUuid);
//            if (hSpotList.size()>0){
//                for (HSpot spot : hSpotList){
//                    spotIds.add(spot.getId());
//                    names.put(spot.getId(),spot.getName());
//                }
//            }
//        }
//        List<SpotTicket> list=null;
//        List<SpotTicket> spotTicketList = Lists.newArrayList();
//        if (CollectionUtils.isNotEmpty(spotIds)){
//            list = spotTicketMapper.selectByUuids(rowBounds, spotIds,deptUuid);
//
//            for (SpotTicket aList : list) {
//                SpotTicket ticket = new SpotTicket();
//                ticket.setId(aList.getId());
//                ticket.setDescription(aList.getDescription());
//                ticket.setUuid(aList.getUuid());
//                ticket.setCreateUser(aList.getCreateUser());
//                ticket.setModifyUser(aList.getModifyUser());
//                ticket.setCode(aList.getCode());
//                ticket.setCreateTime(aList.getCreateTime());
//                ticket.setModifyTime(aList.getModifyTime());
//                ticket.setValid(aList.getValid());
//                ticket.setLabel(aList.getLabel());
//                ticket.setNumber(aList.getNumber());
//                ticket.setPrice(aList.getPrice());
//                ticket.setType(aList.getType());
//                ticket.setSpotName(names.get(aList.getUuid()));
//
//                spotTicketList.add(ticket);
//            }
//        }
//        // uuid  是景点ID
//        FleaPage<SpotTicket> hSpotFleaPage = new FleaPage<>(spotTicketList);
//        hSpotFleaPage.setCurPageData(spotTicketList);
        List<HSpot> spots = null;
        if (!spotName.equals("")){
            StringBuilder sb = new StringBuilder();
            sb.append("%");
            sb.append(spotName);
            sb.append("%");
            spotName = String.valueOf(sb);
            spots = hSpotMapper.selectByName(spotName);
            if (CollectionUtils.isEmpty(spots)){
                return ServerResponse.createBySuccess(new FleaPage<SpotTicket>());
            }
        }
        List<SpotTicket> spotTicketList = spotTicketMapper.selectByDeptId(deptUuid,rowBounds,spotId,spots);
        FleaPage<SpotTicket> hSpotFleaPage = new FleaPage<>(spotTicketList);
        return ServerResponse.createBySuccess(hSpotFleaPage);
    }

    @Override
    public ServerResponse<FleaPage<SpotTicket>> selectBygDeptId(int curPage, int pageSize, String spotName) {
        if (!spotName.equals("")){
            StringBuilder sb = new StringBuilder();
            sb.append("%");
            sb.append(spotName);
            sb.append("%");
            spotName = String.valueOf(sb);
        }
        RowBounds rowBounds = new RowBounds(curPage,pageSize);
        //获取所有供应商
        List<OrgDept> serviceTypes = orgDeptMapper.findByServiceType(HConstant.D_SERVICE_ORG_SUPPLIER);
        if (CollectionUtils.isEmpty(serviceTypes)){
            return ServerResponse.createByErrorMessage("没有找到所有供应商信息");
        }
        List<SpotTicket> data = spotTicketMapper.selectByDeptIdList(serviceTypes,rowBounds,spotName);
        FleaPage<SpotTicket> fleaPage = new FleaPage<>(data);
        return ServerResponse.createBySuccess(fleaPage);
    }

    @Override
    public boolean deleteByIds(List<String> ids, String userId) {
        int result = spotTicketMapper.deleteByIds(ids,userId);
        return result>0;
    }

    @Override
    public FleaPage<SpotTicket> selectAll(String userId, int curPage, int pageSize) {
        RowBounds bounds = new RowBounds(curPage,pageSize);
        OrgDept currentUserDept = FleaOrgUtils.getCurrentUserDept(userId);
        //通过deptId先找到景区，通过景区找门票
        //todod  查询条件需要传入deptId

        List<SpotTicket> spotTicketList = spotTicketMapper.selectAll(bounds);
        FleaPage<SpotTicket> page = new FleaPage<>(spotTicketList);;
        return page;
    }
    @Override
    public ServerResponse<String> saveOrUpdate(SpotTicket entity, String userId, String spotId) {
        OrgDept currentUserDept = FleaOrgUtils.getCurrentUserDept(userId);
        if (currentUserDept==null){
            return ServerResponse.createByErrorMessage("部门信息不能为空");
        }
        String deptUuid = currentUserDept.getDeptUuid();
        String id = entity.getId();
        entity.setModifyUser(userId);
        int result;
        if (StringUtil.isEmpty(id)) {
            //判断是否存在相同类型的门票，存在不能创建
            String ticketType = entity.getType();
            if (StringUtils.isEmpty(ticketType)){
                return ServerResponse.createByErrorMessage("门票类型不能为空");
            }
            List<SpotTicket> list = spotTicketMapper.selectBySpotIdAndDeptId(spotId,deptUuid,ticketType);
            if (list.size()>0){
                return ServerResponse.createByErrorMessage("该类型门票已经存在");
            }
            id = FleaStringUtils.getGenerateUUID();
            entity.setId(id);
            entity.setUuid(spotId);
            entity.setCreateUser(userId);
            entity.setDeptId(deptUuid);
            result = spotTicketMapper.insertSelective(entity);
            if(result!=1){
                return ServerResponse.createByErrorMessage("添加门票失败");
            }
        }else {
            result = spotTicketMapper.updateByPrimaryKeySelective(entity);
            if(result!=1){
                return ServerResponse.createByErrorMessage("更新门票失败");
            }
        }
        return ServerResponse.createBySuccessMessage("添加成功");
    }
}
